var GoodsList = {
    init:function(_path, _id){
        this.path = _path;
        this.id = _id;
        this.page = 0;
        this.pageCount = 10;
        this.total = 0;
        this.innser = $("#goods-college");
        this.btnChangeInner = $("#paging>.pagination");
        console.log(this.path+"\n"+this.id+"\n"+"\n"+this.inner);
        this.queryGoods(0);
    },
    queryGoods:function(page){
        if(page < 0){
            console.log("page 不能小于零");
        }
        $.post(this.path+"/goods/query.do",{
            "id" : GoodsList.id,
            "start" : page*GoodsList.pageCount,
            "count" : GoodsList.pageCount
        },function(data, status){
            if(status === 'success'){
                if(data !== null && data.code === 200){
                    console.log("获取商品信息成功");
                    console.log(data);
                    GoodsList.total = data.total;
                    GoodsList.insertData(data.data);
                    GoodsList.changePagingInf(data.total);
                    return;
                }
            }
            console.log("获取商品信息失败");
        });
    },
//     <td>四川轻化工大学</td>
//     <td>临港校区</td>
//     <td>B8-C229</td>
//     <td>
//          <a class="chat" data-toggle="tooltip" data-placement="left" title="1393886097 点击联系他"
//              href="tencent://message/?uin=1767234937&Site=校园图书&Menu=yes">
//              <div class="qqChat" style="background-image: url('${path}/images/qqChat.png');"></div>
//              1393886097
//          </a>
//      </td>
//      <td>
//          <div class="chat">
//              <div class="phoneChat" style="background-image: url('${path}/images/phoneChat.png');"></div>
//              15583789495
//          </div>
//      </td>
//      <td>3.5</td>
//      <td>2019/03/12</td>
//      <td>借书</td>
    createInner:function(good){
        var tr = $("<tr></tr>");
        tr.append($("<td></td>").text(good.college));
        tr.append($("<td></td>").text(good.village));
        tr.append($("<td></td>").text(good.tower+"-"+good.room));

        var qq = $("<a></a>").addClass("chat").attr("data-toggle","tooltip").attr("data-placement","left").attr("title",good.qq+" 点击联系他")
            .attr("href","tencent://message/?uin="+good.qq+"&Site=校园图书&Menu=yes");
        qq.html("<div class=\"qqChat\" style=\"background-image: url('"+GoodsList.path+"/images/qqChat.png');\"></div><span>1393886097</span>");
        tr.append($("<td></td>").append(qq));
        var phone = $("<div></div>").addClass("chat").html("<div class=\"phoneChat\" style=\"background-image: url('"+
            this.path+"/images/phoneChat.png');\"></div><span>15583789495</span>");
        tr.append($("<td></td>").append(phone));
        tr.append($("<td></td>").text(good.price));
        tr.append($("<td></td>").text(new Date(good.time).toDateString()));
        tr.append($("<td></td>").text("借书"));
        return tr;
    },
    insertData:function(data){
        GoodsList.innser.html('');
        data.forEach(function(good){
            console.log("添加一个数据前");
            GoodsList.innser.append(GoodsList.createInner(good));
            console.log("添加一个数据后");
        });
    },
//     <ul class="pagination">
//          <li>
//              <a><img class="pageIcon" src="${path}/images/firstPage.png" /></a>
//          </li>
//          <li>
//              <a><span aria-hidden="true">&laquo;</span></a>
//          </li>
//          <li><a>1</a></li>
//          <li><a>2</a></li>
//          <li><a>3</a></li>
//          <li><a>4</a></li>
//          <li><a>5</a></li>
//          <li>
//              <a><span aria-hidden="true">&raquo;</span></a>
//          </li>
//          <li>
//              <a><img class="pageIcon" src="${path}/images/lastPage.png" /></a>
//          </li>
//     </ul>
    changePagingInf:function(total){
        var inner = GoodsList.btnChangeInner;
        inner.html('');
        //零序开头，最后一页的序号,而不是总页数
        var totalPage = Math.ceil(total/this.pageCount)-1;
        if(this.page === 0){
            inner.append(this.createDisableFirstPage());
            inner.append(this.createDisablePrePage());
        }else{
            inner.append(this.createFirstPage());
            inner.append(this.createPrePage());
        }
        console.log("first and pre");

        var start, end;
        if(totalPage < 0){
            start = 0;
            end = -1;
        }else if(totalPage < 5 ){
            start = 0;
            end = totalPage;
        }else if(this.page <= 2){
            start = 0;
            end = start + 4;
        }else if(this.page >= totalPage-2){
            end = totalPage;
            start = end - 4;
        }else{
            start = this.page - 2;
            end = this.page + 2;
        }
        console.log("start = "+start+"  end = "+end+ " totalPage = "+totalPage+" total = "+total );
        for(var i = start; i <= end && i <= totalPage; i++){
            if(this.page === i){
                inner.append(this.createNowNormalPage(i+1));
            }else{
                inner.append(this.createNormalPage(i+1));
            }
            console.log("number :"+(i+1));
        }

        if(this.page === totalPage || totalPage < 0){
            inner.append(this.createDisableNextPage());
            inner.append(this.createDisableLastPage());
        }else{
            inner.append(this.createNextPage());
            inner.append(this.createLastPage());
        }
        console.log("next and last");
    },
    createFirstPage:function(){
        return $("<li></li>").append($("<a></a>").append($("<img src='' />").attr("src", this.path+"/images/firstPage.png")
            .addClass("pageIcon"))).click(function(){
                GoodsList.page = 0;
                GoodsList.queryGoods(GoodsList.page);
        });
    },
    createDisableFirstPage:function(){
        return $("<li></li>").addClass("disabled").append($("<span></span>").append($("<img src=''/>")
            .attr("src", this.path+"/images/firstPage.png").addClass("pageIcon")));
    },
    createLastPage:function(){
        return $("<li></li>").append($("<a></a>").append($("<img src='' />")
            .attr("src", this.path+"/images/lastPage.png").addClass("pageIcon"))).click(function(){
                GoodsList.page = Math.ceil(GoodsList.total/GoodsList.pageCount)-1;
                console.log(GoodsList.page);
                GoodsList.queryGoods(GoodsList.page);
        });
    },
    createDisableLastPage:function(){
        return $("<li></li>").addClass("disabled").append($("<span></span>").append($("<img src=''/>")
            .attr("src", this.path+"/images/firstPage.png").addClass("pageIcon")));
    },
    createPrePage:function(){
        return $("<li></li>").append($("<a></a>").append("<span></span>")
            .attr("aria-hidden", "true").html("&laquo;")).click(function(){
                GoodsList.page--;
                GoodsList.queryGoods(GoodsList.page);
        });
    },
    createDisablePrePage:function(){
        return $("<li></li>").addClass("disabled").append($("<span></span>").append("<span></span>")
            .attr("aria-hidden", "true").html("&laquo;"));
    },
    createNextPage:function(){
        return $("<li></li>").append($("<a></a>").append("<span></span>")
            .attr("aria-hidden", "true").html("&raquo;")).click(function(){
                GoodsList.page++;
                GoodsList.queryGoods(GoodsList.page);
        });
    },
    createDisableNextPage:function(){
        return $("<li></li>").addClass("disabled").append($("<span></span>").append("<span></span>")
            .attr("aria-hidden", "true").html("&raquo;"));
    },
    createNormalPage:function(page){
        return $("<li></li>").append($("<a></a>").text(page)).click(function(){
            GoodsList.page = page-1;
            GoodsList.queryGoods(GoodsList.page);
        });
    },
    createNowNormalPage:function(page){
        return $("<li></li>").addClass("active").append($("<a></a>").text(page));
    }
};